<?php

/**
 * GetListApi.php 
 * 
 *
 * @author     late.xiao@qq.com
 */
namespace Sysblog\Api;

use Base\Lib\Implement\Api;

class GetListApi implements Api {

    /**
     * @var string
     * */
    public $api_desc = '根据条件获取文章列表';
    public function getParams()
    {
        $params = array(
                'keyword'=>array('type'=>'string','valid'=>'', 'default'=>'', 'title'=>'文章关键字'),
                'node_id'=>array('type'=>'string','valid'=>'', 'default'=>'', 'title'=>'文章所属类目', 'example'=>'1,2 | 2'),
                'ifpub'=>array('type'=>'int','valid'=>'', 'default'=>'', 'title'=>'文章类型'),
                'fields'=>array('type'=>'string','valid'=>'', 'default'=>'*', 'title'=>'所返回的字段'),
                'page_no'=>array('type'=>'int','valid'=>'', 'default'=>1, 'title'=>'页码'),
                'page_size'=>array('type'=>'string','valid'=>'', 'default'=>10, 'title'=>'每页显示数目'),
                'order'=>array('type'=>'string','valid'=>'', 'default'=>'', 'title'=>'排序'),
        );
        
        return $params;
    }
    
    public function getList($params)
    {
        $filter = array();
        $ifpub = array(0,1);
        if($params['keyword'])
        {
            $filter['title'] = array('LIKE', '%'.$params['keyword'].'%');
        }
        
        if($params['ifpub'] && in_array($params['ifpub'], $ifpub))
        {
            $filter['ifpub'] = $params['ifpub'];
        }
        
        
        if($params['node_id'])
        {
            $node_ids = explode(',', $params['node_id']);
            if(count($node_ids) == 1)
            {
                $filter['node_id'] = intval($node_ids[0]);
            }
            else
            {
                $filter['node_id'] = array('in', $node_ids);
            }
        }
        $field = '*';
        if($params['fields'])
        {
            $field = $params['fields'];
        }
        
        // 处理分页
        $obj_serv = D('Sysblog/Article','Service');
        $count = $obj_serv->count($filter);
        if(!$count)
        {
            return false;
        }
        $total = ceil($count/$params['page_size']);
        
        list($start,$limit,$page) = page_limit($params['page_no'], $params['page_size'],20,$total);

        $data = $obj_serv->getList($filter, $field, $start, $limit, $params['order']);
        $data['total'] = $total;
        
        return $data;
    }

}